package com.zhglxt.oa.activitiListeners.taskListener;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.VariableScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @ClassName BusinessTripExecutionListener
 * @Description 执行监听器
 * @Author moyongjun
 * @Date 2022/5/21 15:55
 * @Version 1.0
 **/
@Component("businessTripExecutionListener")
public class BusinessTripExecutionListener implements Serializable, ExecutionListener {
    /**
     * activitid的表达式对象，流程监听的字段，xml中、可视化编辑器中设置
     */
    private Expression express;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    HistoryService historyService;

    private final Logger logger= LoggerFactory.getLogger(BusinessTripExecutionListener.class);

    public Expression getExpression() {
        return express;
    }

    public void setExpression(Expression express) {
        this.express = express;
    }

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        // VariableScope是父接口，DelegateExecution是VariableScope的子接口
        VariableScope variableScope = delegateExecution;
        Object value = express.getValue(variableScope);
        logger.info("流程执行器获取的value是：{}",value);
        String processInstanceId = delegateExecution.getProcessInstanceId();
        logger.info("processInstanceId:{}",processInstanceId);
//        HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId);
//
//        HistoricProcessInstance historicProcessInstance = historicProcessInstanceQuery.singleResult();
//
//        String startUserId = historicProcessInstance.getStartUserId();
//        logger.info("startUserId:{}",startUserId);

        String eventName = delegateExecution.getEventName();
        logger.info("eventName是：{}",eventName);
    }
}
